package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.adapter.ItemPopChooseAdapter;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.ItemChooseBean;
import com.wnhz.work.yz.bean.VideoItemBean;
import com.wnhz.work.yz.bean.VideoItemTypeBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---视频
 */
public class VideoActivity extends BaseActivity implements View.OnClickListener, TabLayout.OnTabSelectedListener {
    private TextView title;
    private TextView tv_right;
    private List<ItemChooseBean> types = new ArrayList<>();
    private String type;

    private RecyclerView recycler_title;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);

        initData();
        initTitle();
        initView();
    }

    private TabLayout myTabLayout;
    private RecyclerView my_recyclerView;

    private void initView() {
        myTabLayout = (TabLayout) findViewById(R.id.myTabLayout);
        my_recyclerView = (RecyclerView) findViewById(R.id.my_recyclerView);
        // recycler_title = (RecyclerView) findViewById(R.id.recycler_title);
        my_recyclerView.setLayoutManager(new GridLayoutManager(this, 2, LinearLayoutManager.VERTICAL, false));
        myTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        myTabLayout.setOnTabSelectedListener(this);
//

        upload();
    }

    private List<VideoItemBean> titleBean = new ArrayList<>();
    private List<Map<String, Object>> titles = new ArrayList<>();

    private void upload() {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        XUtil.Post(Url.WENKU_GETVIDEOCLASSLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                closeDialog();
                Log.e("----", result);
                super.onSuccess(result);
                try {
                    JSONObject res = new JSONObject(result);
                    String resNum = res.optString("result");
                    if ("1".equals(resNum)) {
                        titleBean.clear();
                        JSONArray info = res.optJSONArray("info");
                        for (int i = 0; i < info.length(); i++) {
                            JSONObject itemInfo = (JSONObject) info.get(i);
                            VideoItemBean bean = new VideoItemBean();
                            String id = itemInfo.optString("id");
                            bean.setId(id);
                            String name = itemInfo.optString("class_name");
                            bean.setClass_name(name);
                            String sort = itemInfo.optString("sort");
                            bean.setSort(sort);
                            titleBean.add(bean);
                            ItemChooseBean bean1 = new ItemChooseBean();
                            bean1.setClicked(false);
                            bean1.setClass_name(name);
                            types.add(bean1);

                        }
                        for (int i = 0; i < titleBean.size(); i++) {
                            Log.e("----222", titleBean.get(i).getClass_name());
                            myTabLayout.addTab(myTabLayout.newTab().setText(titleBean.get(i).getClass_name()).setTag(titleBean.get(i).getId()));
                        }
                        typeAdapter.notifyDataSetChanged();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }


    private PopupWindow popWindow;
    private WindowManager.LayoutParams lp;

    private String caId;
    private static final String TAG = "VideoActivity";

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        type = (String) tab.getTag();
//        index = (int) tab.getTag();
        Log.i(TAG, "onTabSelected: caId" + type);
        uploadGoods();
    }

    private void showSelectPop(View v) {
        View view = LayoutInflater.from(this).inflate(R.layout.pop_choose, null);
        final GridView grid = (GridView) view.findViewById(R.id.gv_pop_choose);

        final ItemPopChooseAdapter itemPopChooseAdapter = new ItemPopChooseAdapter(this, types);
        grid.setAdapter(itemPopChooseAdapter);

        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String chooseText = types.get(position).getTitles();//选中的条目

                int size = types.size();
                for (int i = 0; i < size; i++) {
                    if (i == position) {
                        types.get(i).setClicked(true);
                        myTabLayout.setScrollPosition(position, 0, true);
                        caId = titleBean.get(position).getId();
//                        Log.i(TAG, "onTabSelected: caId1" + caId);
                        uploadGoods();

                    } else {
                        types.get(i).setClicked(false);
                    }
                }
                itemPopChooseAdapter.notifyDataSetChanged();

                popWindow.dismiss();
            }
        });

    }

    private void initData() {
//        textColorNor = getResources().getColor(R.color.text_two);
//        textColorCli = getResources().getColor(R.color.main_color);
    }

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("视频");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
        }

    }


    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    List<VideoItemTypeBean> viewItemBean = new ArrayList<>();

    private void uploadGoods() {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("type", type);
        XUtil.Post(Url.WENKU_GETINDEXVIDEOLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                Log.e(type + "----3333", result);
                try {
                    JSONObject res = new JSONObject(result);
                    String resNum = res.optString("result");
                    viewItemBean.clear();
                    if ("1".equals(resNum)) {
                        JSONArray arr = res.optJSONArray("info");

                        for (int i = 0; i < arr.length(); i++) {
                            /**
                             *       "video_id": "4",
                             "url": "http://youzhu.unohacha.com//Api/Api/playVideo/id/4.html",
                             "img": "http://youzhu.unohacha.com/Uploads/Picture/uploads/20170118/587ebdc432d00.png",
                             "title": "华住集团汉庭款1720科逸产品推介",
                             "describe": "华住集团汉庭款1720科逸产品推介",
                             "goods_id": "295",
                             "type": "1"
                             */
                            JSONObject typeObj = (JSONObject) arr.opt(i);
                            VideoItemTypeBean typeBean = new VideoItemTypeBean();
                            typeBean.setDescribe(typeObj.getString("describe"));
                            typeBean.setVidoo_id(typeObj.getString("video_id"));
                            typeBean.setUrl(typeObj.getString("url"));
                            typeBean.setImg(typeObj.getString("img"));
                            typeBean.setTitle(typeObj.getString("title"));
                            typeBean.setGoods_id(typeObj.getString("goods_id"));
                            typeBean.setType(typeObj.getString("type"));
                            viewItemBean.add(typeBean);
                        }
                        typeAdapter.notifyDataSetChanged();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                super.onSuccess(result);
            }

            @Override
            public void onFinished() {
                closeDialog();
                setTypeItemData();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

    BaseRVAdapter<VideoItemTypeBean> typeAdapter = new BaseRVAdapter<VideoItemTypeBean>(this, viewItemBean) {
        @Override
        public int getLayoutId(int viewType) {
            return R.layout.frag_video_item;
        }

        @Override
        public void onBind(BaseViewHolder holder, final int position) {
            x.image().bind(holder.getImageView(R.id.iv_img), viewItemBean.get(position).getImg());
            holder.setTextView(R.id.tv_title, viewItemBean.get(position).getTitle());
            holder.setTextView(R.id.tv_content, viewItemBean.get(position).getDescribe());
            holder.getView(R.id.max_ll).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(VideoActivity.this, PlayVideoActivity.class);
                    intent.putExtra("img", viewItemBean.get(position).getImg());
                    intent.putExtra("title", viewItemBean.get(position).getTitle());
                    intent.putExtra("des", viewItemBean.get(position).getDescribe());

                    intent.putExtra("videoUrl", viewItemBean.get(position).getUrl());
                    startActivity(intent);
                }
            });
        }

    };


    private void setTypeItemData() {

        my_recyclerView.setAdapter(typeAdapter);
//        my_recyclerView.
    }


}
